import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CreateCode {
    public static void main(String[] args) {
        System.out.print("是否开始自动生成(y)");
        if (new Scanner(System.in).next().equalsIgnoreCase("y") == false) {
            return;
        }
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        final String projectPath = System.getProperty("user.dir");// +"/sv-member/";
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("雄哥爱吃肉");
        gc.setOpen(false);

        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(true);// XML columList
        // ָ指定各层类名规则
        gc.setMapperName("%sDao");// Dao类名
        gc.setServiceName("%sService");// 业务层接口名
        gc.setServiceImplName("%sServiceImpl");// 业务层实现类名
        gc.setControllerName("%sController");// controller的类名
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:oracle:thin:@localhost:1521:orcl");//
        dsc.setDriverName("oracle.jdbc.driver.OracleDriver");//
        dsc.setUsername("liuyang");//
        dsc.setPassword("123");//
        mpg.setDataSource(dsc);

        // 包配置
        final PackageConfig pc = new PackageConfig();
        pc.setModuleName("");
        pc.setParent("com.gec.it");
        pc.setMapper("dao");
        mpg.setPackageInfo(pc);

        // 要想输出xml文件，需要额外自定义添加进去
        String templatePath = "/mapper.xml.btl";
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };
        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/"
                        + tableInfo.getEntityName() + "Mapper.xml";
            }
        });

        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // strategy.setInclude(可选择要生成的表名，多个英文逗号分割);
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setEntityTableFieldAnnotationEnable(true);
        strategy.setFieldPrefix("DEPTS_", "JOBS_","DOCTORS_","PLANS_","PERSONS_","BOOKS_","POWER_");
        strategy.setInclude("HP_DEPTS", "HP_JOBS","HP_DOCTORS","HP_PLANS","HP_PERSONS","HP_BOOKS","HP_POWER");
        strategy.setTablePrefix("HP_");

        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new BeetlTemplateEngine());
        mpg.execute();
        System.out.println("成功");
    }

}
